




var _oConst =
{
sInvalidSchemaNameChars:		"[^A-Za-z0-9_]",

sCheckBoxIdFormat:				"chkDA{0}",

sEntityEditorUrlFormat:			"manageEntity.aspx?entityId={0}",
sInformationPageId: 			"divInformation",
sUIElementsPageId: 				"divUIElements",
sAttributesPageId: 				"divAttributes",
sRelationshipsPageId1ToN:		"divRelationships1ToN",
sRelationshipsPageIdNTo1: 		"divRelationshipsNTo1",
sRelationshipsPageIdNToN: 		"divRelationshipsNToN",
sMessagesPageId:				"divMessages"
};






var _oMessages =
{
sPublishAlert:	LOCID_MANENT_PUBLISHALERT,
sCloseAlert:	LOCID_FORMS_SAVE_CONFIRM_TITLE

};






var _bSaving = false;
var _sInputXml = "";











function ManageEntityJsWindowOnLoad()
{
if (!_bCreate)
{
window.focus();
}

_sInputXml = getEntityXml();

loadPage(_oConst.sInformationPageId);

if (!txtSingularName.Disabled)
{
txtSingularName.SetFocus();
}
}

window.attachEvent('onload', ManageEntityJsWindowOnLoad);




window.attachEvent("onbeforeunload", window_onbeforeunload);
function window_onbeforeunload()
{
if (!_bSaving && isFormDirty())
{
event.returnValue = _oMessages.sCloseAlert;
}
}




function document.onkeydown()
{
return ProcessKeyDown();
}

function _save()
{
saveEntityAction(false);
}

function _saveandclose()
{
saveEntityAction(true);
}

function _saveandnew()
{
}










function CallbackParams(bClose, bReload, bRefreshGrid, bRerfreshMsgGrid)
{
this.bClose				= bClose;
this.bReload			= bReload;
this.bRefreshGrid		= bRefreshGrid;
this.bRerfreshMsgGrid	= bRerfreshMsgGrid;
}

function remoteEntityCommandCallback(oResult, oCallbackParams)
{
if (oResult.Success)
{

if (oResult.RemoteCommand.Command == _oEntUtlConst.sCreateEntityCommand)
{
_sEntityId = oResult.ReturnValue;
}
else if (oResult.RemoteCommand.Command == _oEntUtlConst.sDeleteEntityCommand)
{
if (!IsNull(oResult.ReturnValue) && oResult.ReturnValue.Used)
{
showUsage(_oSCUConst.sEntityMode, oResult.ReturnValue);
return true;
}
}


if (oCallbackParams.bRefreshGrid)
{
try
{
window.opener.gridEntities.Refresh();
}
catch(e)
{}
}


if (oCallbackParams.bClose)
{
window.close();
return false;
}


if (oCallbackParams.bReload)
{

frmReload.action = formatString(_oConst.sEntityEditorUrlFormat, _sEntityId);
frmReload.submit();
return false;
}


if (oCallbackParams.bRerfreshMsgGrid)
{
refreshMsgGrid();
}
}

_bSaving = false;
return true;
}




function saveEntityAction(bClose)
{
if (!_bSaving && validateParameters())
{
_bSaving = true;

var sDataXml = getEntityXml();
var oCallbackParams = new CallbackParams(bClose, true, true, false);

if (_bCreate)
{
createEntity(sDataXml, remoteEntityCommandCallback, oCallbackParams);
}
else if (isFormDirty())
{
updateEntity(sDataXml, remoteEntityCommandCallback, oCallbackParams);
}
else if (bClose)
{
window.close();
return;
}
else
{
_bSaving = false;
}
}
}




function deleteEntityAction()
{
var oCallbackParams = new CallbackParams(true, false, true, false);
deleteEntity(_sEntityId, true, remoteEntityCommandCallback, oCallbackParams);
}




function publishEntityAction()
{
if (!isFormDirty() || confirm(_oMessages.sPublishAlert))
{
var oDataXml = createXmlDoc(_oTags.publish);
var oEntitiesXml = addXmlNode(oDataXml, _oTags.entities);
addTextXmlNode(oEntitiesXml, _oTags.entity, _sEntityId);
var sDataXml = convertXmlDocToString(oDataXml);
var oCallbackParams = new CallbackParams(false, false, false, true);

publishEntities(sDataXml, 1, remoteEntityCommandCallback, oCallbackParams);
}
}




function updateEntityIconsAction()
{
updateEntityIcons(_sEntityId, true);
}




var _oVisiblePage = null;
function loadPage(sPageId)
{
var oPage = document.all(sPageId);


if (_oVisiblePage != null)
{
_oVisiblePage.style.display = "none";
}


_oVisiblePage = oPage;
_oVisiblePage.style.display = "inline";


if (sPageId != _oConst.sInformationPageId)
{
var oIFrameElement	= oPage.children(0);
var oIFrame			= document.frames(oIFrameElement.id);
if (oIFrame.location.href.indexOf("/_root/Blank.aspx") > -1)
{
oIFrame.location = oIFrameElement.url;
}
}


if(event.srcElement)
{
ResetBreadcrumb(event.srcElement);
}

}


function ResetBreadcrumb(selectedNav)
{

var imgNavigation = findNavigationComponent(selectedNav, "IMG");
var textNavigation = findNavigationComponent(selectedNav, "NOBR");
var imgBreadcrumb = document.all("leftNavBreadcrumbImg");
var textBreadcrumb = document.all("leftNavBreadcrumbText");
if ((imgBreadcrumb != null) && (imgNavigation != null))
{
imgBreadcrumb.src = imgNavigation.src;
imgBreadcrumb.style.backgroundImage = imgNavigation.style.backgroundImage;
imgBreadcrumb.style.backgroundPositionY = imgNavigation.style.backgroundPositionY;
imgBreadcrumb.style.backgroundPositionX = imgNavigation.style.backgroundPositionX;
imgBreadcrumb.style.clip = imgNavigation.style.clip;
imgBreadcrumb.style.height = imgNavigation.style.height;
imgBreadcrumb.style.width = imgNavigation.style.width;
}
if ((textBreadcrumb != null) && (textNavigation != null))
{
textBreadcrumb.innerText = textNavigation.innerText;
}
}

function findNavigationComponent(eventSourceElement, elementTagName)
{

var		parentElement = null;
if ("A" == eventSourceElement.tagName)
{
parentElement = eventSourceElement;
}
else
{
parentElement = eventSourceElement.parentElement;
}
return parentElement.getElementsByTagName(elementTagName)[0];
}









function getEntityXml()
{
var oDataXml = createXmlDoc(_oTags.entity);

addTextXmlNode(oDataXml, _oTags.entityid, _sEntityId);
addSchemaXml(oDataXml);
addDisplayAreaXml(oDataXml);
addRichClientXml(oDataXml);
addDuplicateDetectionXml(oDataXml);
addAssociatedEntityXml(oDataXml);
addPrimaryAttributeXml(oDataXml);

return convertXmlDocToString(oDataXml);
}




function addSchemaXml(oDataXml)
{
addControlDataValue(oDataXml, _oTags.singularname,  txtSingularName);
addControlDataValue(oDataXml, _oTags.pluralname,    txtPluralName);
addControlDataValue(oDataXml, _oTags.schemaname,    txtSchemaName);
addControlDataValue(oDataXml, _oTags.ownershiptype, selOwnershipType);
addControlDataValue(oDataXml, _oTags.description,   txtDescription);
}




function addDisplayAreaXml(oDataXml)
{
var oDisplayAreasXml = addXmlNode(oDataXml, _oTags.displayareas);

for (var iCheckBox = 0;; iCheckBox++)
{
var oCheckBox = document.all(formatString(_oConst.sCheckBoxIdFormat, iCheckBox));

if (IsNull(oCheckBox))
{
break;
}

if (!oCheckBox.Disabled)
{
var oDisplayAreaXml = addXmlNode(oDisplayAreasXml, _oTags.displayarea);
addXmlAttribute(oDisplayAreaXml, _oTags.id, oCheckBox.areaid);
addControlDataValueAsAttribute(oDisplayAreaXml, _oTags.displayed, oCheckBox);
}
}
}




function addDuplicateDetectionXml(oDataXml)
{
addControlDataValue(oDataXml, _oTags.duplicatecheck, chkEnableDupCheck);
}




function addRichClientXml(oDataXml)
{
var oRCXml = addXmlNode(oDataXml, _oTags.richclient);
addControlDataValue(oRCXml, _oTags.offline, chkRCOffline);
}




function addAssociatedEntityXml(oDataXml)
{
var oAEXml = addXmlNode(oDataXml, _oTags.associatedentity);

addControlDataValue(oAEXml, _oTags.notes, chkAENotes);
addControlDataValue(oAEXml, _oTags.activities, chkAEActivities);
}




function addPrimaryAttributeXml(oDataXml)
{
if (_bCreate)
{
var oAttributeXml = addXmlNode(oDataXml, _oTags.attribute);

addControlDataValue(oAttributeXml, _oTags.displayname,	txtAttributeDisplayName);
addControlDataValue(oAttributeXml, _oTags.schemaname,	txtAttributeSchemaName);
addControlDataValue(oAttributeXml, _oTags.reqlevel,		selAttributeReqLevel);
addControlDataValue(oAttributeXml, _oTags.description,	txtAttributeDescription);

var oTypeXml = addXmlNode(oAttributeXml, _oTags.type);

addControlDataValue(oTypeXml, _oTags.name,		selAttributeType);
addControlDataValue(oTypeXml, _oTags.format,	selAttributeFormat);
addControlDataValue(oTypeXml, _oTags.length,	numMaxAttributeLen);
}
}










function validateParameters()
{
var bValid = true;

bValid = bValid && validateRequiredValue(txtSingularName);
bValid = bValid && validateRequiredValue(txtPluralName);

if (_bCreate)
{
bValid = bValid && validateSchemaName (txtSchemaName);
bValid = bValid && validateRequiredValue(txtAttributeDisplayName);
bValid = bValid && validateSchemaName(txtAttributeSchemaName);
bValid = bValid && validateRequiredValue(selAttributeReqLevel);
bValid = bValid && validateRequiredValue(numMaxAttributeLen);
}

return bValid;
}






function onSingularNameChange()
{
if (_bCreate &&
(txtSingularName.DataValue != null && txtSingularName.DataValue.length > 0) &&
(txtSchemaName.DataValue == null || txtSchemaName.DataValue.length == 0))
{
var regExp = new RegExp(_oConst.sInvalidSchemaNameChars, "g");
txtSchemaName.DataValue = txtSingularName.DataValue.replace(regExp, "").substr(0, txtSchemaName.maxLength);

if(txtSchemaName.DataValue != null && txtSchemaName.DataValue.length > 0)
{
txtSchemaName.DataValue = txtSchemaName.DataValue.toLowerCase();
}
}
}

function onAttributeSchemaNameChange()
{
if (_bCreate &&
(txtAttributeDisplayName.DataValue != null && txtAttributeDisplayName.DataValue.length > 0) &&
(txtAttributeSchemaName.DataValue == null || txtAttributeSchemaName.DataValue.length == 0))
{
var regExp = new RegExp(_oConst.sInvalidSchemaNameChars, "g");
txtAttributeSchemaName.DataValue = txtAttributeDisplayName.DataValue.replace(regExp, "").substr(0, txtAttributeSchemaName.maxLength);

txtAttributeSchemaName.DataValue = txtAttributeSchemaName.DataValue.toLowerCase();
}
}






function isFormDirty()
{
return _sInputXml != getEntityXml();
}

function refreshMsgGrid()
{
try
{
var oPage			= document.all(_oConst.sMessagesPageId);
var oIFrameElement	= oPage.children(0);
var oIFrame			= document.frames(oIFrameElement.id);

oIFrame.document.all("gridDisplayStrings").Refresh();
}
catch(e)
{}
}
